package com.bbc.commonbackstage.common.utils;

import com.bbc.commonbackstage.common.exception.ServiceException;
import org.springframework.lang.Nullable;
import org.springframework.util.CollectionUtils;
import org.springframework.util.ObjectUtils;
import org.springframework.util.StringUtils;

import java.util.Collection;

public class Assert extends org.springframework.util.Assert {

    public static void isTrue(boolean expression, Integer code, String message) {
        if (!expression) {
            throw new ServiceException(message, code);
        }
    }

    public static void isFalse(boolean expression, Integer code, String message) {
        if (expression) {
            throw new ServiceException(message, code);
        }
    }

    public static void isNull(@Nullable Object object, Integer code, String message) {
        if (object != null) {
            throw new ServiceException(message, code);
        }
    }

    public static void notNull(@Nullable Object object, Integer code, String message) {
        if (object == null) {
            throw new ServiceException(message, code);
        }
    }

    public static void hasLength(@Nullable String text, Integer code, String message) {
        if (!StringUtils.hasLength(text)) {
            throw new ServiceException(message, code);
        }
    }

    public static void hasText(@Nullable String text, Integer code, String message) {
        if (!StringUtils.hasText(text)) {
            throw new ServiceException(message, code);
        }
    }

    public static void notEmpty(@Nullable Object[] array, Integer code, String message) {
        if (ObjectUtils.isEmpty(array)) {
            throw new ServiceException(message, code);
        }
    }

    public static void notEmpty(@Nullable Collection<?> collection, Integer code, String message) {
        if (CollectionUtils.isEmpty(collection)) {
            throw new ServiceException(message, code);
        }
    }

}
